package com.moxiao.yfjh.module.blind.controller.admin.basicSetting;

import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.basicSetting.BlindBasicSettingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.basicSetting.BlindBasicSettingDO;
import com.moxiao.yfjh.module.blind.service.basicSetting.BlindBasicSettingService;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲基础设置")
@RestController
@RequestMapping("/blind/basic-setting")
@Validated
public class BlindBasicSettingController {

    @Resource
    private BlindBasicSettingService basicSettingService;


//    @MemberSubmissionAddressing
    @PostMapping("/create")
    @Operation(summary = "创建相亲基础设置")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:create')")
    public CommonResult<Long> createBasicSetting(@Valid @RequestBody BlindBasicSettingCreateReqVO createReqVO) {
        return success(basicSettingService.createBasicSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲基础设置")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:update')")
    public CommonResult<Boolean> updateBasicSetting(@Valid @RequestBody BlindBasicSettingUpdateReqVO updateReqVO) {
        basicSettingService.updateBasicSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲基础设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:delete')")
    public CommonResult<Boolean> deleteBasicSetting(@RequestParam("id") Long id) {
        basicSettingService.deleteBasicSetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲基础设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:query')")
    public CommonResult<BlindBasicSettingRespVO> getBasicSetting(@RequestParam("id") Long id) {
        BlindBasicSettingDO basicSetting = basicSettingService.getBasicSetting(id);
        return success(BlindBasicSettingConvert.INSTANCE.convert(basicSetting));
    }


    @GetMapping("/list")
    @Operation(summary = "获得相亲基础设置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:query')")
    public CommonResult<List<BlindBasicSettingRespVO>> getBasicSettingList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindBasicSettingDO> list = basicSettingService.getBasicSettingList(ids);
        return success(BlindBasicSettingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲基础设置分页")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:query')")
    public CommonResult<PageResult<BlindBasicSettingRespVO>> getBasicSettingPage(@Valid BlindBasicSettingPageReqVO pageVO) {
        PageResult<BlindBasicSettingDO> pageResult = basicSettingService.getBasicSettingPage(pageVO);
        return success(BlindBasicSettingConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲基础设置 Excel")
    @PreAuthorize("@ss.hasPermission('blind:basic-setting:export')")
    @OperateLog(type = EXPORT)
    public void exportBasicSettingExcel(@Valid BlindBasicSettingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlindBasicSettingDO> list = basicSettingService.getBasicSettingList(exportReqVO);
        // 导出 Excel
        List<BlindBasicSettingExcelVO> datas = BlindBasicSettingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲基础设置.xls", "数据", BlindBasicSettingExcelVO.class, datas);
    }

}
